package com.arvato.wxwork.controller.admin.kpi;


import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.arvato.minipro.bean.bean_dto.common.PageData;
import com.arvato.minipro.bean.bean_dto.common.Query;
import com.arvato.minipro.bean.bean_dto.common.Result;
import com.arvato.wxwork.bean.bean_do.kpi.WxworkKpiSetDo;
import com.arvato.wxwork.bean.bean_vo.kpi.WxworkKpiSetVo;
import com.arvato.wxwork.service.kpi.WxworkKpiSetService;


/**
 * 企业微信KPI设定Controller
 * @author zhan655
 *
 */
@Controller
@RequestMapping("/admin/wxwork/kpi/set")
public class WxworkKpiSetController {
	
	private Logger logger = LoggerFactory.getLogger(WxworkKpiSetController.class);

	@Autowired
	private WxworkKpiSetService wxworkKpiSetService;

	/**
	 * 获取kpi设定列表
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/list")
	public PageData listKpiSet(HttpServletRequest req, @RequestParam Map<String, Object> param) {
		Query query = new Query(param);
		logger.info("获取kpi设定列表");
		PageData pageData = wxworkKpiSetService.listKpiSetByQuery(query, query.getPageNum(), query.getPageSize());
		return pageData;
	}
	
	/**
	 * 获取一个kpi的详情
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/detail")
	public Result KpiSetDetail(String id) {
		WxworkKpiSetVo KpiSetVo = wxworkKpiSetService.getKpiSetById(id);
		return Result.success(KpiSetVo);
	}
	
	/**
	 * 创建kpi设定
	 * @param req
	 * @param KpiSetDo
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/insert")
	public Result create(HttpServletRequest req, String[] kpiSetList) {
		logger.info("新增一个kpi设定");
		Calendar cale = Calendar.getInstance();  
        int year = cale.get(Calendar.YEAR);  
        int month = cale.get(Calendar.MONTH) + 1;  
        int day = cale.get(Calendar.DATE);
        List<WxworkKpiSetDo> failList = new ArrayList<WxworkKpiSetDo>();
		for(String item : kpiSetList) {
			WxworkKpiSetDo kpiSetDo = new WxworkKpiSetDo();
			kpiSetDo.setPositionCode(item.split("_")[0]);
			kpiSetDo.setKpiCodeRow(item.split("_")[1]);
			kpiSetDo.setKpiCodeColumn(item.split("_")[2]);
			kpiSetDo.setDefaultValue(item.split("_")[3]);
			kpiSetDo.setYear(year + "");
			kpiSetDo.setMonth(month + "");
			kpiSetDo.setDay(day + "");
			kpiSetDo.setCreateTime(new Date());
			try {
				wxworkKpiSetService.insert(kpiSetDo);
			} catch(DuplicateKeyException e) {
				failList.add(kpiSetDo);
				logger.info("新增kpi设定失败",e);
			} catch (Exception e) {
				e.printStackTrace();
				logger.info("新增kpi设定失败",e);
			}
		}
		if(failList.size() >= 1) {
			String msg = "如下kpi设定失败:</br>";
			for(int i = 0; i < failList.size(); i ++) {
				WxworkKpiSetDo wxworkKpiSetDo = failList.get(i);
				msg += "(" + i + ")" + "职位编号：" + wxworkKpiSetDo.getPositionCode() + "</br>"
						+ "KPI行编号：" + wxworkKpiSetDo.getKpiCodeRow() + "</br>"
						+ "KPI列编号：" + wxworkKpiSetDo.getKpiCodeColumn() + "</br>"
						+ "年份：" + wxworkKpiSetDo.getYear() + "</br>"
						+ "月份：" + wxworkKpiSetDo.getMonth() + "</br>";
			}
			return Result.fail(msg); 
		}
		return Result.success();
	}
	
	/**
	 * 更新kpi
	 * @param req
	 * @param KpiSetDo
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/update")
	public Result update(HttpServletRequest req, WxworkKpiSetDo KpiSetDo) {
		logger.info("更新一个kpi");
		try {
			wxworkKpiSetService.update(KpiSetDo);
		} catch(DuplicateKeyException e) {
			return Result.fail("新增失败,kpi编号:" + KpiSetDo + "已存在");
		} catch (Exception e) {
			e.printStackTrace();
			return Result.fail("更新kpi失败：" + e.getMessage());
		}
		return Result.success("更新kpi成功。");
	}
	
	/**
	 * 删除kpi
	 * @param req
	 * @param KpiSetId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/delete")
	public Result delete(HttpServletRequest req, String id) {
		logger.info("删除一个kpi节点");
		try {
			wxworkKpiSetService.delete(id);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.fail("更新kpi失败：" + e.getMessage());
		}
		return Result.success("更新kpi成功。");
	}
	
}

